﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Input;

namespace Jewelnoid
{
    class Credits : SceneBase
    {
        private InputController input;
        private Background background;
        private List<List<Block>> blocksList;
        private int maxLine;
        private int maxColumn;
        private Text2D credits;
        private Text2D back;
        private Boolean endCredits;
        private double time;
        private int blockIndexI;
        private int blockIndexJ;

        public override void start()
        {
            SpriteFont spritefont = Game1.self.Content.Load<SpriteFont>("8BitWonder");
            Texture2D blockTexture = Game1.self.Content.Load<Texture2D>("Block");
            String backText = "Back";
            Vector2 backScale = new Vector2(0.5f, 0.5f);
            String creditsText = "This game was developed by\nLeonardo Faria,\nTeo Lahmeyer\nand Renan Selleri.\n\nThanks for playing!\nWe hope to see you soon!";
            Vector2 creditsScale = new Vector2(0.25f, 0.25f);

            this.input = new InputController();
            this.background = new Background(Game1.self.Content.Load<Texture2D>("Background"));
            this.blocksList = new List<List<Block>>();
            this.maxLine = Game1.self.graphics.PreferredBackBufferHeight / (blockTexture.Height + 5);
            this.maxColumn = Game1.self.graphics.PreferredBackBufferWidth / (blockTexture.Width + 5);
            this.back = new Text2D(spritefont, backText, new Vector2(Game1.self.graphics.PreferredBackBufferWidth/2 - (spritefont.MeasureString(backText).X * backScale.X)/2, 3 * Game1.self.graphics.PreferredBackBufferHeight/4), Color.White);
            this.back.scale = backScale;
            this.credits = new Text2D(spritefont, creditsText, new Vector2(Game1.self.graphics.PreferredBackBufferWidth / 2 - (spritefont.MeasureString(creditsText).X * creditsScale.X) / 2, Game1.self.graphics.PreferredBackBufferHeight / 4), Color.White);
            this.credits.scale = creditsScale;
            this.back.color = Color.Red;
            this.endCredits = false;
            this.time = 0;
            this.blockIndexI = maxLine - 3;
            this.blockIndexJ = maxColumn - 3;

            Vector2 startposition = new Vector2(blockTexture.Width / 2 + 2, blockTexture.Height / 2 + 7);
            Color randomColor = Level.genericColor(7); //Porque eu gosto do número 7. O valor da seed não importa aqui, pois só ocorrerá 1 vez ao iniciar o Menu principal.

            for (int i = 0; i < maxLine; i++)
            {
                List<Block> blockLine = new List<Block>();

                randomColor = Block.nextColor(randomColor);

                for (int j = 0; j < maxColumn; j++)
                {
                    blockLine.Add(new Block(blockTexture, new Vector2(j * (blockTexture.Width + 5) + startposition.X, i * (blockTexture.Height + 5) + startposition.Y), randomColor));

                    if (!(i == 0 || j == 0 || i == 1 || j == 1 || i == maxLine - 1 || j == maxColumn - 1 || i == maxLine - 2 || j == maxColumn - 2))
                    {
                        blockLine[j].alpha = 0;
                    }
                }

                this.blocksList.Add(blockLine);
            }

            SoundManager.resumeMusic();
        }

        public override void terminate()
        {
            background = null;
            blocksList = null;

            SoundManager.pauseMusic();
        }

        public override void draw(SpriteBatch spriteBatch)
        {
            background.draw(spriteBatch);

            this.back.draw(spriteBatch);
            this.credits.draw(spriteBatch);

            if (this.endCredits)
            {
                this.back.alpha -= 0.02f;
                this.credits.alpha -= 0.02f;
            }

            for (int i = 0; i < maxLine; i++)
            {
                for (int j = 0; j < maxColumn; j++)
                {
                    blocksList[i][j].draw(spriteBatch);
                }
            }
        }

        public override void update(GameTime gameTime)
        {
            if (!this.endCredits)
            {
                this.input.getState();

                if (this.input.isKeyJustPressed(Keys.Enter) || this.input.isMouseLeftButtonJustPressed(this.back))
                {
                    this.endCredits = true;
                }
            }
            else
            {
                if (endAnimation(gameTime))
                {
                    SceneManager.setScene(new Menu());
                }
            }
        }

        /// <summary>
        ///   Return true if the animation end
        /// </summary>
        /// <param name="gameTime"></param>
        /// <returns></returns>
        public Boolean endAnimation(GameTime gameTime)
        {
            this.blocksList[this.blockIndexI][this.blockIndexJ].alpha = 1;
         
            if (this.blocksList[this.blockIndexI][this.blockIndexJ - 1].alpha != 1 && this.blocksList[this.blockIndexI + 1][this.blockIndexJ].alpha != 0)
            {
                this.blockIndexJ--;
            }
            else if (this.blocksList[this.blockIndexI - 1][this.blockIndexJ].alpha != 1)
            {
                this.blockIndexI--;
            }
            else if (this.blocksList[this.blockIndexI][this.blockIndexJ + 1].alpha != 1)
            {
                this.blockIndexJ++;
            }
            else if (this.blocksList[this.blockIndexI + 1][this.blockIndexJ].alpha != 1)
            {
                this.blockIndexI++;
            }                
            else
            {
                return true;
            }

            return false;
        }
    }
}
